package com.dbConnector;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.HttpServlet;
import javax.sql.DataSource;

public class DataBaseConnector extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
	private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/e_ticaret";
	private static final String DB_USER = "root";
	private static final String DB_PASSWORD = "root";
 
	private static Connection dbConnection = null;
	static DataSource ds=null;
	static {
	
		try{
		
			Context initContext = new InitialContext();
			Context envContext = (Context) initContext.lookup("java:comp/env");
			ds = (DataSource) envContext.lookup("jdbc/e_ticaret");
			
		}catch (Exception e) {
		
			e.printStackTrace();
		}
		
		
	}
 
	public static Connection getDBConnection() {
 
		try {
             
	
			dbConnection = ds.getConnection();
			
			//Class.forName(DB_DRIVER);
 
		} catch (Exception e) {
 
			e.printStackTrace();
 
		}
 
//		try {
// 
//			dbConnection = DriverManager.getConnection(
//                             DB_CONNECTION, DB_USER,DB_PASSWORD);
//			return dbConnection;
// 
//		} catch (Exception e) {
// 
//			e.printStackTrace();
// 
//		}
 
		return dbConnection;
 
	}

	public Connection getDbConnection() {
		return dbConnection;
	}
	
	public void setDbConnection(Connection dbConnection) {
		this.dbConnection = dbConnection;
	}
	

}
